/**
 * 
 */
package com.informatica.powercenter.sdk.mapfwk.samples;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.informatica.powercenter.sdk.mapfwk.repository.PmrepRepositoryConnectionManager;
import com.informatica.powercenter.sdk.mapfwk.repository.RepoPropsConstants;
import com.informatica.powercenter.sdk.mapfwk.repository.Repository;
import com.informatica.powercenter.sdk.mapfwk.repository.RepositoryConnectionManager;

/**
 * @author sramamoo
 *
 */
public class TestConcurrency
{
	static int counter = 0;
	public void init(Repository rep) throws IOException
	{
		Properties properties = new Properties();
    	String filename = "pcconfig.properties";
    	InputStream propStream = getClass().getClassLoader().getResourceAsStream( filename);

    	if ( propStream != null ) {
    		properties.load( propStream );
	        rep.getRepoConnectionInfo().setPcClientInstallPath(properties.getProperty(RepoPropsConstants.PC_CLIENT_INSTALL_PATH));
	        rep.getRepoConnectionInfo().setTargetFolderName(properties.getProperty(RepoPropsConstants.TARGET_FOLDER_NAME));
	        rep.getRepoConnectionInfo().setTargetRepoName(properties.getProperty(RepoPropsConstants.TARGET_REPO_NAME));
	        rep.getRepoConnectionInfo().setRepoServerHost(properties.getProperty(RepoPropsConstants.REPO_SERVER_HOST));
	        rep.getRepoConnectionInfo().setAdminPassword(properties.getProperty(RepoPropsConstants.ADMIN_PASSWORD));
	        rep.getRepoConnectionInfo().setAdminUsername(properties.getProperty(RepoPropsConstants.ADMIN_USERNAME));
	        rep.getRepoConnectionInfo().setRepoServerPort(properties.getProperty(RepoPropsConstants.REPO_SERVER_PORT));
	        rep.getRepoConnectionInfo().setServerPort(properties.getProperty(RepoPropsConstants.SERVER_PORT));
	        rep.getRepoConnectionInfo().setDatabaseType(properties.getProperty(RepoPropsConstants.DATABASETYPE));
	        
	        if(properties.getProperty(RepoPropsConstants.PMREP_CACHE_FOLDER) != null)
	        	rep.getRepoConnectionInfo().setPmrepCacheFolder(properties.getProperty(RepoPropsConstants.PMREP_CACHE_FOLDER));	        	
        }
        else {
            throw new IOException( "pcconfig.properties file not found.");
        }
	}
	public static void main(String[] args) throws IOException
	{		
		TestConcurrency test = new TestConcurrency();
		Repository rep = new Repository( "PowerCenter", "PowerCenter", "This repository contains API test samples" );   
		RepositoryConnectionManager repMgr = new PmrepRepositoryConnectionManager();
		
		test.init(rep);
        
		Thread[] threads = new Thread[5];
		for(int i=0;i<5;i++)
		{
			threads[i] = new Thread(new function(i,repMgr, rep));
			threads[i].start();
		}		
	}
}
class function implements Runnable
{
	int id;
	RepositoryConnectionManager repMgr;
	Repository rep;
	public function(int id, RepositoryConnectionManager repMgr, Repository rep)
	{
		this.id = id;
		this.repMgr = repMgr;
		this.rep = rep;
	}
	public void run()
	{
		ModifyObjectsSample sample = null;
		try
		{
			sample = new ModifyObjectsSample();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		sample.setMapFileName("ModifyObjects_"+id);
		sample.setRepository(rep);
		sample.setRepositoryConnectionManager(repMgr);
		try
		{
			sample.execute();
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}		
}